package com.docMan.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.docMan.utils.FinalConfig;
import com.docMan.utils.ImageScale;
import com.docMan.utils.ProjectUtils;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport implements SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3287470960305478122L;
	protected Map<String ,Object> session;
	protected void write(Map<String, Object> map){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html");
		//String mapJsonString=JSON.toJSONString(map, true);
		String jsonString=new Gson().toJson(map);
		try {
			PrintWriter out=response.getWriter();
			out.write(jsonString);
            out.flush();
            out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void write(String map){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html");
		//String mapJsonString=JSON.toJSONString(map, true);
		try {
			PrintWriter out=response.getWriter();
			out.write(map);
            out.flush();
            out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}
	
	public Map<String, Object> getSession(){
		return session;
	}
	protected Object get(String key){
		return  session.get(key); 
	}
	
	protected void put(String key,Object value){
		session.put(key, value);
	}
	/**
	 *  
	 * @param file
	 * @param i
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	@SuppressWarnings("unused")
	protected void uploadFile(final List<String> fileNames, final List<File> file)
			throws FileNotFoundException, IOException {
		String fileName=ProjectUtils.date2String("yyyy-MM-dd", new Date());
		String path=FinalConfig.STRUTS_UPLOAD_PATH+fileName;
		File file2=new File(path); 
		if(!file2.exists()){
			file2.mkdir();
		}
		for (int i = 0; i < file.size(); i++) {
			InputStream in = new FileInputStream(file.get(i));
			@SuppressWarnings("deprecation")
			String dir = ServletActionContext.getRequest().getRealPath(
					FinalConfig.STRUTS_UPLOAD_PATH);
			File uploadFile = new File(path,
					fileNames.get(i));
			OutputStream out = new FileOutputStream(uploadFile);
			byte[] buffer = new byte[1024 * 1024 * 2];
			int length;
			while ((length = in.read(buffer)) > 0) {
				out.write(buffer, 0, length);
			}
			in.close();
			out.close();
		}
		Thread imageScale=new Thread(new Runnable() {
			@Override
			public void run() {
				String name="";
				String fileName=ProjectUtils.date2String("yyyy-MM-dd", new Date());
				String path=FinalConfig.STRUTS_UPLOAD_PATH+fileName+"/";
				for (int i = 0; i < file.size(); i++) {
					int pos=fileNames.get(i).indexOf('.');
					int len=fileNames.get(i).length();
					name=fileNames.get(i).substring(0, pos)+FinalConfig.PIC_SIZE_240_240+fileNames.get(i).substring(pos, len);
					try {
						ImageScale.resizeFix(new File(path+fileNames.get(i)), new File(path+name), 240, 240);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});
		imageScale.run();
	}
	
}
